package com.focus.pay.ali.constant;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.focus.pay.ali.config.AliPayConfig;
import com.focus.pay.core.exception.ExceptionEnum;
import com.focus.pay.core.exception.PayException;
import com.focus.pay.core.utils.AssertUtils;
import com.focus.pay.core.utils.StringUtils;
import lombok.Getter;

/**
 * @author 赵参谋
 * @version $
 * =========================================================================
 * 变更履历：
 * -------------------------------------------------------------------------
 * 变更编号     变更时间    变更人   变更原因    变更内容
 * -------------------------------------------------------------------------
 * <p>
 * @description：支付宝常量
 * @date 2021/12/31 10:01
 **/
public class Constant {

    @Getter
    public enum AliPayPath{
        PRECREATE               ("alipay.trade.precreate","统一收单线下交易预创建"),
        CREATE                  ("alipay.trade.create","统一收单交易创建接口"),
        QUERY                   ("alipay.trade.query","统一收单线下交易查询"),
        REFUND                  ("alipay.trade.refund","统一收单交易退款接口"),
        REFUND_QUERU            ("alipay.trade.fastpay.refund.query","统一收单交易退款查询"),
        DEPOSITBACK_COMPLETED   ("alipay.trade.refund.depositback.completed","收单退款冲退完成通知"),
        CANCEL                  ("alipay.trade.cancel","统一收单交易撤销接口"),
        CLOSE                   ("alipay.trade.close","统一收单交易关闭接口"),
        ;
        private String path;

        private String desc;

        AliPayPath(String path,String desc){
            this.path=path;
            this.desc=desc;
        }
    }

    @Getter
    public enum AliPayDomain {
        //沙箱环境
        ALIPAY_GATEWAY_DOMAIN_DEV("https://openapi.alipaydev.com/gateway.do"),
        //生产网关
        ALIPAY_GATEWAY_DOMAIN_PRO("https://openapi.alipay.com/gateway.do"),
        ;

        private String domain;

        AliPayDomain(String domain) {
            this.domain = domain;
        }
    }

    /**
     * 支付宝加密方式
     */
    public enum AliPayEncryptionType{
        RSA2,  //SHA256WithRSA
        RSA    //SHA1WithRSA
    }


    public static AlipayClient buildAlipayClient(AliPayConfig aliPayConfig) throws PayException {

        String appId = aliPayConfig.getAppId(),
                privateKey = aliPayConfig.getPrivateKey(),
                format = aliPayConfig.getFormat(),
                charset = aliPayConfig.getCharset(),
                pubicKey = aliPayConfig.getPublicKey(),
                signType = aliPayConfig.getSignType();
        //参数校验
        AssertUtils.notEmpty(appId, () -> new PayException(ExceptionEnum.VALID_ERROR));
        AssertUtils.notEmpty(privateKey, () -> new PayException(ExceptionEnum.VALID_ERROR));
        AssertUtils.notEmpty(pubicKey, () -> new PayException(ExceptionEnum.VALID_ERROR));
        if (StringUtils.isEmpty(format)) {
            format = "JSON";
        }
        if (StringUtils.isEmpty(charset)) {
            charset = "UTF8";
        }
        if (StringUtils.isEmpty(signType)) {
            signType = "RSA2";
        }
        if (aliPayConfig.isDev())
            return new DefaultAlipayClient(AliPayDomain.ALIPAY_GATEWAY_DOMAIN_DEV.getDomain(), appId, privateKey, format, charset, pubicKey, signType);
        else
            return new DefaultAlipayClient(AliPayDomain.ALIPAY_GATEWAY_DOMAIN_PRO.getDomain(), appId, privateKey, format, charset, pubicKey, signType);
    }
}